package com.platform.cdcs.fragment;

import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.platform.cdcs.R;
import com.platform.cdcs.fragment.choose.DealerChooseFragment;
import com.platform.cdcs.model.BaseObjResponse;
import com.platform.cdcs.model.CustomerItem;
import com.platform.cdcs.model.MockObj;
import com.platform.cdcs.model.RefershEvent;
import com.platform.cdcs.model.XtbmItem;
import com.platform.cdcs.tool.Constant;
import com.platform.cdcs.tool.FragmentUtil;
import com.platform.cdcs.tool.ViewTool;
import com.trueway.app.uilib.fragment.BaseFragment;
import com.trueway.app.uilib.tool.Utils;
import com.trueway.app.uilib.widget.TwDialogBuilder;
import com.zhy.http.okhttp.callback.StringCallback;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import okhttp3.Call;

/**
 * Created by holytang on 2017/9/28.
 */
public class ApplyDetailFragment extends BaseFragment {

    private CustomerItem item;
    private View rootView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        item = (CustomerItem) getArguments().getSerializable("model");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void initView(View rootView) {
        initLoadImg(rootView.findViewById(R.id.load));
        this.rootView = rootView;
        setTitle("代报申请详情");
        getToolBar().setNavigationIcon(R.mipmap.icon_back);
        getToolBar().setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().finish();
            }
        });
        rootView.findViewById(R.id.text).setVisibility(View.GONE);
        if (item != null) {
            setData(item);
        } else {
            requestDetail();
        }
    }

    private void setData(CustomerItem item) {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        LinearLayout root1 = (LinearLayout) rootView.findViewById(R.id.button1);

        ViewTool.setFourItem(inflater, root1, new String[]{"代报经销商代码", item.getDistApplyCode()});
        ViewTool.setFourItem(inflater, root1, new String[]{"代报经销商名称", item.getDistApplyName()});
        ViewTool.setFourItem(inflater, root1, new String[]{"是否合并库存", item.getIsMerge() == 1 ? "合并" : "不合并"});
        LinearLayout root2 = (LinearLayout) rootView.findViewById(R.id.button2);
        String state = "申请失败";
        if ("1".equals(item.getStatus())) {
            state = "申请成功";
        } else if ("0".equals(item.getStatus())) {
            state = "申请中";
        }
        ViewTool.setFourItem(inflater, root2, new String[]{"状态", "处理结果"}, new String[]{state, item.getResult_msg()});
        ViewTool.setFourItem(inflater, root2, new String[]{"备注", item.getRemark()});

        LinearLayout root3 = (LinearLayout) rootView.findViewById(R.id.button3);
        ViewTool.setFourItem(inflater, root3, new String[]{"申请时间", "审核时间"}, new String[]{item.getFill_date(), item.getUpdate_date()});

        rootView.findViewById(R.id.button4).setVisibility(View.GONE);
        rootView.findViewById(R.id.button5).setVisibility(View.GONE);
    }

    @Override
    public int layoutId() {
        return R.layout.five_layout;
    }

    private void requestDetail() {
        showLoadImg();
        Map<String, String> map = new HashMap<>();
        map.put("id", getArguments().getString("id"));
        getHttpClient().post().url(Constant.OTHER_REPORT_INFO).params(Constant.makeParam(map)).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int i) {
                dismissLoadImg();
                Utils.showToast(getContext(), R.string.server_error);
            }

            @Override
            public void onResponse(String s, int i) {
                try {
                    if (getContext() == null) {
                        return;
                    }
                    dismissLoadImg();
                    Type type = new TypeToken<BaseObjResponse<CustomerItem.CusList>>() {
                    }.getType();
                    BaseObjResponse<CustomerItem.CusList> response = new Gson().fromJson(s, type);
                    if ("1".equals(response.getResult().getCode())) {
                        setData(response.getResult().getOtherReportInfo());
                    } else {
                        Utils.showToast(getContext(), response.getResult().getMsg());
                    }
                } catch (Exception e) {
                    Utils.showToast(getContext(), "解析数据失败");
                }
            }
        });
    }

}
